import adraw.*;
import geometry.*;
import tester.*;
import tester.cobertura.Instrument;
import java.util.*;
import colors.*;

/**
 * Star 
 * @author Ryan & Adam
 */
@Instrument
class Star{
  int VEL = 8;
  int span;
  Posn location;
  int rad = 4;
  
  /**
   * Constructor
   * @param span The Stars lifespan created between 5 and 35
   * @param location The Stars location
   */
  Star(int span, Posn location) {
    this.span = span;
    this.location = location;
  }
  /*
   * TEMPLATE:
   *  FIELDS:
   * ... this.VEL ...           --int
   * ... this.location ...      --Posn
   * ... this.span ...          --int
   * ... this.rad ...           --int
   *
   *  METHODS:
   * ... this.onTick() ...             -- void
   * ... this.makeStar() ...           -- Star
   * ... this.hitGroundp() ...         -- boolean
   * ... this.isFoundp(Mfmwg m) ...    -- boolean
   * ... this.drawStar(Canvas c) ...   -- void
   */
  
  /**
   * Draws the Star at its location with the set radium and color.
   * @param c
   */
  void drawStar(Canvas c){
    c.drawDisk(this.location, this.rad, new Yellow());
  }
  
  /**
   * Moves the star downwards and shortens span on one tick.
   */
  void onTick(){
    this.span--;
    this.location = new Posn(this.location.x, this.location.y + this.VEL);
  }
  
  /**
   * Has this Star hit the Genie?
   * @param m
   * @return
   */
  boolean isFoundp(Mfmwg m){
    return Math.sqrt((this.location.x - m.location.x) * 
                     (this.location.x - m.location.x) +
                     (this.location.y - m.location.y) * 
                     (this.location.y - m.location.y)) <
                     (m.rad + this.rad);
  }
  
  /**
   * Has this star hit the ground?
   * @return
   */
  boolean hitGroundp(){
    return this.location.y >= 300;
  }
  
  /**
   * Makes a new star at a random x-pos, with a random lifespan, at the top of
   * the canvas.
   * @return
   */
  Star makeStar(){
    return new Star(Math.abs(new Random().nextInt() % 30) + 5,
                    new Posn(Math.abs(new Random().nextInt() % 195) + 5, 5));
  }
}